//app.js
// import {authorization} from "./api/index"
App({
    onLaunch: function () {
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        this.wxLogin()
    },
    // 微信登录
    wxLogin() {
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                // authorization({wxCode:res.code})
                if (res.code) {
                    this.globalData.wxCode = res.code;
                    wx.setStorageSync('wxCode', res.code)
                    // 服务端登录
                    this.login(res.code)
                } else {
                    wx.showToast({
                        title: "登录失败，请稍后再试",
                        icon: 'none',
                        duration: 1500
                    })
                }
            }
        })
    },
    // 服务端登录
    login(wxCode) {
        let _this = this;
        wx.request({
            url: `${this.globalData.baseUrl}/dealer/getUserInfoByCode?code=${wxCode}`,
            method: "POST",
            header: { 'content-type': 'application/json' },
            success(res) {
                wx.hideLoading()
                let { data, success } = res.data
                if (success) {
                    if (data.token) wx.setStorageSync('token', data.token)
                    _this.globalData.openid = data.openId
                    _this.globalData.session_key = data.session_key
                    if (data.id) {
                        _this.globalData.userInfo = data
                        _this.globalData.userId = data.id
                        _this.globalData.phone = data.mobile
                        _this.globalData.token = data.token
                        _this.globalData.username = data.username
                        // wx.navigateTo({
                        //     url: `/pages/home/home`
                        // })
                    } 
                    // 暂时隐藏默认登录页面
                    // else if (data.openId) {
                    //     wx.navigateTo({
                    //         url: `/pages/login/login`
                    //     })
                    // }
                }
            }
        })
    },
    noLogin() {
        wx.hideLoading()
        const data = {
            "openId": "oMP2a5SIbrh3pVqY95FXUDWbhgDg",
            "remark": "微信用户",
            "type": "ELECTRONIC_UNAUTHORIZED",
            "delFlag": "0",
            "type_dic": "非授权电工",
            "lastLoginTime_str": "2020-12-06 13:48:50",
            "password": "$2a$10$8KpG6CnbJ/K01ahwrECwveis.lLvIb36HZ6Ui5.xiedtTWZSkLx0G",
            "session_key": "ePISCIpnNN7Sug+ZdR+o8A==",
            "nickname": "测试添加电工端",
            "sysAttachments": [],
            "id": "11935da7f8d14eda9c414ee354732cfc",
            "state": "1",
            "address": "110000,110100,110101",
            "headImg": "http://sinotruk.oss-cn-qingdao.aliyuncs.com/2020-12-06/80ddfd4b08864b96af727cd1a8bde094.png",
            "mobile": "17662532975",
            "updateTime": "2020-12-06 13:48:50",
            "sort": 999,
            "createTime_str": "2020-12-06 13:48:50",
            "lastLoginTime_date_str": "2020-12-06",
            "token": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJhY2NvdW50SWQiOiIxMTkzNWRhN2Y4ZDE0ZWRhOWM0MTRlZTM1NDczMmNmYyIsInJvbGVzIjpbXSwiZXhwIjoxNjA4OTc5NDc4fQ.32LTmrrUd-YRyQhSsCxaiRrkEK5vPeboVw2_Ax2vMGpFjD67ZRraAeSv7Xv7KeptHcWm7GUOK8pM405SHVRwQQ",
            "updateTime_date_str": "2020-12-06",
            "lastLoginTime": "2020-12-06 13:48:50",
            "createTime": "2020-12-06 13:48:50",
            "updateTime_str": "2020-12-06 13:48:50",
            "category": "in",
            "createTime_date_str": "2020-12-06",
            "username": "17662532975"
        }
        wx.setStorageSync('token', data.token)
        this.globalData.userInfo = data
        this.globalData.openid = data.openId
        this.globalData.session_key = data.session_key
        this.globalData.userId = data.id
        this.globalData.phone = data.mobile
        this.globalData.token = data.token
        this.globalData.username = data.username
        wx.navigateTo({
            url: `/pages/home/home`
        })
    },
    globalData: {
        title: "尚驰海尔驻车空调",
        userInfo: null,
        // baseUrl: "http://127.0.0.1:6891",
        baseUrl: "https://erp.shangchi4006999166.com/__api",
        wxCode: undefined,
        openid: undefined,
        sessionKey: undefined,
        token: null,
        phone: undefined,
        isRegister: 0
    },
    // 更新页面title
    upTitle(title) {
        wx.setNavigationBarTitle({
            title: title
        })
    },
    gotoDetailsPage(url, id) {
        if (this.globalData.token && this.globalData.token.length > 0) {
            wx.navigateTo({
                url: `${url}?id=${id}`
            })
        } else {
            let token = wx.getStorageSync("token")
            if (token && token.length > 0) {
                this.globalData = token
                wx.navigateTo({
                    url: `${url}?id=${id}`
                })
            } else {
                this.wxLogin()
                // wx.navigateTo({
                //   url:`/pages/index/index`
                // })
            }
        }

    }
})

// sercrt: 3b3f8add84f022e7887d3015d545bcdf
// 106b7631ab82ac6bc703712bf60dcd74
// appId:wx321c4f08e435114a
// wx155fd74a68d87df3
